package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    @ApiModelProperty("状态码")
    private Integer state;      //状态码
    @ApiModelProperty("操作失败时的描述文本")
    private String message;     //操作失败时的描述文本
    @ApiModelProperty("操作成功时响应的数据")
    private T data;        //操作成功时响应的数据

    public static JsonResult ok(){
        return ok(null);
    }

    public static<T> JsonResult<T> ok(T data){
        JsonResult<T> jr = new JsonResult();
        jr.state = ServiceCode.OK.getValue();
        jr.setData(data);
        return jr;
    }

    public static JsonResult fail(ServiceException e){
        return fail(e.getCode(), e.getMessage());
    }

    public static JsonResult fail(ServiceCode serviceCode, String message){
        JsonResult jr = new JsonResult();
        jr.state=serviceCode.getValue();
        jr.message=message;
        return jr;
    }
}